package cn.supermarket.service.impl;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.StockCheckMapper;
import cn.supermarket.pojo.dto.StockCheckAddDTO;
import cn.supermarket.pojo.dto.StockCheckUpdateDTO;
import cn.supermarket.pojo.entity.StockCheck;
import cn.supermarket.pojo.vo.StockCheckDetailsVo;
import cn.supermarket.pojo.vo.StockDetailedVO;
import cn.supermarket.service.iservice.ICheckStockService;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.JsonPage;
import cn.supermarket.web.State;
import com.alipay.api.domain.StockHolderVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class StockCheckServiceImpl implements ICheckStockService {
    @Autowired
    private StockCheckMapper stockCheckMapper;
    @Override
    public void addInfo(StockCheckAddDTO stockCheckAddDTO) {
        System.out.println("新增库存盘点");
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker=new SnowFlakeGenerateIdWorker(1,2);
        Long id=snowFlakeGenerateIdWorker.nextId();
        StockCheck stockCheck=new StockCheck();
        BeanUtils.copyProperties(stockCheckAddDTO,stockCheck);
        stockCheck.setId(id);
        stockCheck.setCheckPeople(stockCheckAddDTO.getCheckPeople());
        stockCheck.setCount(stockCheckAddDTO.getCount());
        stockCheck.setNumber(stockCheckAddDTO.getNumber());
        stockCheck.setProductId(stockCheckAddDTO.getProductId());
        stockCheck.setProductName(stockCheckAddDTO.getProductName());
        String name=stockCheckAddDTO.getProductName();
        if (name==null){
            throw new ServiceException(State.ERR_NOT_FOUND,"新增类别失败，请输入商品名称！");
        }
        int i = stockCheckMapper.upload(stockCheck);
        if (i!=1){
            throw new ServiceException(State.ERR_INSERT,"新增库存信息失败,服务器忙,请稍后再次尝试");
        }
    }

    @Override
    public JsonPage<StockCheckDetailsVo> selectListByDate(Integer pageNum, Integer pageSize, Date date) {
        // 执行查询前设置分页条件
        PageHelper.startPage(pageNum,pageSize);
        List<StockCheckDetailsVo>list=stockCheckMapper.selectByDate(date);
        // 实例化PageInfo对象获得分页信息后将它转换为JsonPage返回
        return JsonPage.restPage(new PageInfo<>(list));

    }

//    @Override
//    public void updateRecordByProductId(StockCheckUpdateDTO stockCheckUpdateDTO) {
//        stockCheckMapper.updateByProductId(stockCheckUpdateDTO.getProductId());
//        log.info("修改完毕！");
//
//
//    }






    @Override
    public StockCheckDetailsVo selectDetailsByProductId(Long productId) {
        StockCheckDetailsVo stockCheckDetailsVo = stockCheckMapper.selectByProductId(productId);
        log.info("查询完毕！");
        return stockCheckDetailsVo;


    }

    @Override
    public void deleteByProductId(Long productId) {
        stockCheckMapper.deleteStockByProductId(productId);
        log.info("删除完毕！");
    }


}
